package com.gzhu.knowledgeAdmin.controller;


import com.gzhu.knowledgeAdmin.common.exception.BusinessException;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.entity.Flow;
import com.gzhu.knowledgeAdmin.service.FlowService;
import com.mysql.cj.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class FlowController {

    @Resource
    private FlowService flowService;

    @GetMapping("/flow/{templateId}")
    public R getFlowByTemplateId(@PathVariable Integer templateId){
        if (templateId == null || templateId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }
        List<Flow> flowList = flowService.getFlowByTemplateId(templateId);
        return R.ok(flowList);
    }

    @PostMapping("/flow")
    public R getTargetId(Integer templateId, String answerType){
        if (templateId == null || templateId < 1) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }
        if (StringUtils.isNullOrEmpty(answerType)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }
        int targetId = flowService.getTargetId(templateId, answerType);
        return R.ok(targetId);
    }



}
